Skip to content

NumFilterWidget

Base classes

Name Children Inherits
Widget
prettyqt.widgets.widget
The base class of all user interface objects.

⋔ Inheritance diagram

graph TD
  598574988["custom_widgets.NumFilterWidget"]
  3664736470["widgets.Widget"]
  829357528["widgets.WidgetMixin"]
  3227531497["core.ObjectMixin"]
  2751680898["builtins.object"]
  625385617["gui.PaintDeviceMixin"]
  3620195106["QtWidgets.QWidget"]
  3984060476["QtCore.QObject"]
  37984305["Shiboken.Object"]
  673956333["QtGui.QPaintDevice"]
  3664736470 --> 598574988
  829357528 --> 3664736470
  3227531497 --> 829357528
  2751680898 --> 3227531497
  625385617 --> 829357528
  2751680898 --> 625385617
  2751680898 --> 829357528
  3620195106 --> 3664736470
  3984060476 --> 3620195106
  37984305 --> 3984060476
  2751680898 --> 37984305
  673956333 --> 3620195106
  37984305 --> 673956333

🛈 DocStrings

Bases: Widget

Widget with a button to choose from a comparison op (>=, >, etc) + a lineedit.

Source code in prettyqt/custom_widgets/numfilterwidget.py
class NumFilterWidget(widgets.Widget):
    """Widget with a button to choose from a comparison op (>=, >, etc) + a lineedit."""

    filter_changed = core.Signal(object)

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.set_layout("horizontal", margin=0)
        self.lineedit = widgets.LineEdit()
        self.lineedit.set_validator("double", empty_allowed=True)
        self.op_button = widgets.ToolButton()
        self.op_button.setStyleSheet("QToolButton::menu-indicator{width:0px;}")
        self.box.add(self.op_button)
        self.box.add(self.lineedit)
        self.menu = widgets.Menu(triggered=self._on_menu_click)
        for comp in ["=", "<=", "<", ">=", ">"]:
            self.menu.add_action(comp)
        self.op_button.setMenu(self.menu)
        self.op_button.set_popup_mode("instant")
        self.op_button.setText("=")
        self.lineedit.value_changed.connect(self._on_filter_change)

    def _on_filter_change(self):
        self.filter_changed.emit(self.get_filter_fn())

    def _on_menu_click(self, action: gui.QAction):
        self.op_button.setText(action.text())
        self._on_filter_change()

    def get_filter_fn(self) -> Callable | None:
        val = self.lineedit.get_value()
        if not val:
            return lambda x: True
        try:
            num = float(val)
        except ValueError:
            return None
        match self.op_button.text():
            case "=":
                op = operator.eq
            case "<=":
                op = operator.ge
            case "<":
                op = operator.gt
            case ">=":
                op = operator.le
            case ">":
                op = operator.lt
            case _:
                raise ValueError
        return functools.partial(op, num)

:fa-brands-github: Show source on GitHub